package num14;

import java.util.Arrays;

/**
 * 14. 最长公共前缀
 * me
 */
class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0 || strs == null) {
            return "";
        }

        int min = Integer.MAX_VALUE;
        for (int i = 0; i < strs.length; i++) {
            min = Math.min(min, strs[i].length());
        }

        boolean flag = true;
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < min; i++) {
            char c = strs[0].charAt(i);
            for (int j = 0; j < strs.length; j++) {
                if (c != strs[j].charAt(i)) {
                    flag = false;
                    break;
                }
            }

            if (flag) {
                res.append(c);
            } else {
                break;
            }
        }


        return res.toString();
    }

    public static void main(String[] args) {
        String[] strs = {"f"};
        System.out.println(new Solution().longestCommonPrefix(strs));
    }
}